package com.zheng.api.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.zheng.api.common.constant.ApiConstant;
import com.zheng.api.common.constant.ApiResult;
import com.zheng.api.common.constant.ApiResultConstant;
import com.zheng.api.server.service.ApiCommonService;
import com.zheng.api.server.service.base.um.Base4UmbrellaService;
import com.zheng.common.util.DateUtil;
import com.zheng.common.util.JsonUtil;
import com.zheng.common.util.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Description: 公共控制器
 * @author lvb
 * @date 2017-10-30
 * @version V1.0
 * @Copyright:
 */
@Controller
@Api(value = "签到控制器", description = "签到控制器")
@RequestMapping(value = "/v1/common")
public class ApiCommonController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiCommonController.class);

    @Autowired
    ApiCommonService apiCommonService;
    @Autowired
    Base4UmbrellaService base4UmbrellaService;

    @RequestMapping(value = "/checkIn")
    @ApiOperation(value = "签到")
    @ResponseBody
    public String checkIn(@RequestParam String params) {
        LOGGER.info("###### 开始接收签到请求 {}######",params);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ipAddress = RequestUtil.getIpAddr(request);
        /*
            {
                "command": 20001,
                "machineCode": "2018092700001",
                "code": 1,
                "message": "",
                "data": {
                    "type": 1
                }
            }
         */
        Date beginTime = DateUtil.getDate();
        String command = null;
        String machineCode = null;
        int code = 0;
        String message = null;
        JSONObject dataInfo = new JSONObject();
        ApiResult result = new ApiResult(ApiResultConstant.FAILED.code,ApiResultConstant.FAILED.message, ApiConstant.UMBRELLA_MACHINE_COMMAND_10001,machineCode,dataInfo);
        try {
            JSONObject jsonParams = JsonUtil.getJSONObjectFromJson(params);
            command = jsonParams.getString("command");
            machineCode = jsonParams.getString("machineCode");
            code = jsonParams.getInteger("code");
            message = jsonParams.getString("message");
            JSONObject recvData = jsonParams.getJSONObject("data");
            result = apiCommonService.checkIn(command,machineCode,code,message,recvData);
        }catch(Exception e){
            LOGGER.error("解析通信消息日志失败",e);
        }finally {
            Date endTime = DateUtil.getDate();
            base4UmbrellaService.recordUmbrellaHardwareMqttcommandLog(beginTime,params,endTime,JsonUtil.object2Json(result));
        }
        LOGGER.info("###### 處理签到请求完成 {}######",params);
        return JsonUtil.object2Json(result);
    }

    @RequestMapping(value = "/notify")
    @ApiOperation(value = "通知")
    @ResponseBody
    public String notify(@RequestParam String params) {
        LOGGER.info("###### 开始接收通知请求 {}######",params);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ipAddress = RequestUtil.getIpAddr(request);
        /*
            {
                "command": 20004,
                "machineCode": "2018092700001",
                "code": 1,
                "message": "",
                "data": {
                    "type": 1,
                    "status": 1,// 0建立连接异常，1 建立连接成功
                    "errorMsg"://反馈描述信息或异常信息
                }
            }
         */
        Date beginTime = DateUtil.getDate();
        String command = null;
        String machineCode = null;
        int code = 0;
        String message = null;
        JSONObject dataInfo = new JSONObject();
        ApiResult result = new ApiResult(ApiResultConstant.FAILED.code,ApiResultConstant.FAILED.message, ApiConstant.UMBRELLA_MACHINE_COMMAND_10004,machineCode,dataInfo);
        try {
             JSONObject jsonParams = JsonUtil.getJSONObjectFromJson(params);
             command = jsonParams.getString("command");
             machineCode = jsonParams.getString("machineCode");
             code = jsonParams.getInteger("code");
             message = jsonParams.getString("message");
            JSONObject recvData = jsonParams.getJSONObject("data");
            result = apiCommonService.notify(command,machineCode,code,message,recvData,ipAddress);
        }catch(Exception e){
            LOGGER.error("解析通信消息日志失败",e);
        }finally {
            Date endTime = DateUtil.getDate();
            base4UmbrellaService.recordUmbrellaHardwareMqttcommandLog(beginTime,params,endTime,JsonUtil.object2Json(result));
        }
        LOGGER.info("###### 處理通知请求完成 {}######",params);
        return JsonUtil.object2Json(result);
    }
}
